package cn.iocoder.an.module.business.controller.admin.equipmentkeepconfig;

import cn.iocoder.an.module.business.job.CreateEquipmentWaitJob;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentkeepconfig.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentkeepconfig.EquipmentKeepConfigDO;
import cn.iocoder.an.module.business.service.equipmentkeepconfig.EquipmentKeepConfigService;

@Tag(name = "管理后台 - 设备保养配置")
@RestController
@RequestMapping("/business/equipment-keep-config")
@Validated
public class EquipmentKeepConfigController {

    @Resource
    private EquipmentKeepConfigService equipmentKeepConfigService;


    @Resource
    private CreateEquipmentWaitJob createEquipmentWaitJob;

    @PostMapping("/batchConfig")
    @Operation(summary = "批量配置")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:create')")
    public CommonResult<Integer>  createEquipmentKeepConfig(@RequestBody BindSaveOrDeleteVO vo) {
      return success(equipmentKeepConfigService.batchEquipmentKeepConfig(vo));
    }

    @PostMapping("/create")
    @Operation(summary = "创建设备保养配置")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:create')")
    public CommonResult<Long> createEquipmentKeepConfig(@Valid @RequestBody EquipmentKeepConfigSaveReqVO createReqVO) {
        return success(equipmentKeepConfigService.createEquipmentKeepConfig(createReqVO));
    }

    @GetMapping("/handleJob")
    @Operation(summary = "创建设备保养配置")
    public void handleJob() throws Exception {
        createEquipmentWaitJob.execute("");
    }


    @PutMapping("/update")
    @Operation(summary = "更新设备保养配置")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:update')")
    public CommonResult<Boolean> updateEquipmentKeepConfig(@Valid @RequestBody EquipmentKeepConfigSaveReqVO updateReqVO) {
        equipmentKeepConfigService.updateEquipmentKeepConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备保养配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepConfig(@RequestParam("id") Long id) {
        equipmentKeepConfigService.deleteEquipmentKeepConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备保养配置")
                @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepConfigList(@RequestParam("ids") List<Long> ids) {
        equipmentKeepConfigService.deleteEquipmentKeepConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备保养配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:query')")
    public CommonResult<EquipmentKeepConfigRespVO> getEquipmentKeepConfig(@RequestParam("id") Long id) {
        EquipmentKeepConfigDO equipmentKeepConfig = equipmentKeepConfigService.getEquipmentKeepConfig(id);
        return success(BeanUtils.toBean(equipmentKeepConfig, EquipmentKeepConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备保养配置分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:query')")
    public CommonResult<PageResult<EquipmentKeepConfigRespVO>> getEquipmentKeepConfigPage(@Valid EquipmentKeepConfigPageReqVO pageReqVO) {
        PageResult<EquipmentKeepConfigDO> pageResult = equipmentKeepConfigService.getEquipmentKeepConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentKeepConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备保养配置 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentKeepConfigExcel(@Valid EquipmentKeepConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentKeepConfigDO> list = equipmentKeepConfigService.getEquipmentKeepConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备保养配置.xls", "数据", EquipmentKeepConfigRespVO.class,
                        BeanUtils.toBean(list, EquipmentKeepConfigRespVO.class));
    }

}